package com.iot.ammeter.utils;


import com.iot.ammeter.dto.app.user.AppUserLoginInfoDTO;
import io.jsonwebtoken.Claims;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ClaimsUtil
 * @Author: yjx
 * @Date: 2021/10/25
 * @Version: 1.0
 * @Description: 将登录后的app用户信息封装成map，用于生成token
 **/
public class AppClaimsUtil {

    /**
     * app端用户id
     */
    private static final String APPUSERID = "appUserId";
    /**
     * 用户名
     */
    private static final String USERNAME = "userName";
    /**
     * 邮箱
     */
    private static final String EMAIL = "email";

    /**
     * 将web用户信息封装成map
     * @param appUserLoginInfoDTO app用户信息
     * @return map
     */
    public static Map<String,Object> appUserToClaims(AppUserLoginInfoDTO appUserLoginInfoDTO) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(APPUSERID, appUserLoginInfoDTO.getAppUserId());
        claims.put(USERNAME,appUserLoginInfoDTO.getUserName());
        claims.put(EMAIL,appUserLoginInfoDTO.getEmail());
        return claims;
    }

    /**
     * 解析app端用户信息
     * @param claims 载荷
     * @return appUserLoginInfoDTO
     */
    public static AppUserLoginInfoDTO claimsToAppUserLoginInfo(Claims claims) {
        AppUserLoginInfoDTO AppUserLoginInfoDTO = new AppUserLoginInfoDTO();
        AppUserLoginInfoDTO.setAppUserId(String.valueOf(claims.get(APPUSERID)));
        AppUserLoginInfoDTO.setUserName(String.valueOf(claims.get(USERNAME)));
        AppUserLoginInfoDTO.setEmail(String.valueOf(claims.get(EMAIL)));
        return AppUserLoginInfoDTO;
    }
}
